﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public class UReturn500VisitDAL
    {
        public static int Add(UReturn500VisitInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UReturn500Visit_Add");
            DB.AddInParameter(dbCommand, "UWCID", DbType.Int32, obj.UWCID);
            DB.AddInParameter(dbCommand, "AgentName", DbType.String, obj.AgentName);
            DB.AddInParameter(dbCommand, "VisitType", DbType.String, obj.VisitType);
            DB.AddInParameter(dbCommand, "WorkTime", DbType.Decimal, obj.WorkTime);
            DB.AddInParameter(dbCommand, "XCJJ", DbType.Int16, obj.XCJJ);
            DB.AddInParameter(dbCommand, "JJMY", DbType.Int16, obj.JJMY);
            DB.AddInParameter(dbCommand, "GZTD", DbType.Int16, obj.GZTD);
            DB.AddInParameter(dbCommand, "otherRemark", DbType.String, obj.otherRemark);
            DB.AddInParameter(dbCommand, "ZDLX", DbType.Int16, obj.ZDLX);
            DB.AddInParameter(dbCommand, "JSBY", DbType.Int16, obj.JSBY);
            DB.AddInParameter(dbCommand, "CGZF", DbType.Int16, obj.CGZF);
            DB.AddInParameter(dbCommand, "PJJG", DbType.Int16, obj.PJJG);
            DB.AddInParameter(dbCommand, "U4RID", DbType.Int32, obj.U4RID);

            //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static bool Delete(int ID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UReturn500Visit_Delete", ID);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetList(int ID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UReturn500Visit_GetList", ID);
            return DB.ExecuteReader(dbCommand);
        }
        public static DataTable GetPagination(string whereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UReturn500Visit_GetPagination");
            DB.AddInParameter(dbCommand, "whereSql", DbType.String, whereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTable dt = DB.ExecuteDataSet(dbCommand).Tables[0];

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }
        public static bool Update(UReturn500VisitInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UReturn500Visit_Update");
            DB.AddInParameter(dbCommand, "ID", DbType.Int32, obj.ID);
            DB.AddInParameter(dbCommand, "WorkTime", DbType.Decimal, obj.WorkTime);
            DB.AddInParameter(dbCommand, "XCJJ", DbType.Int16, obj.XCJJ);
            DB.AddInParameter(dbCommand, "JJMY", DbType.Int16, obj.JJMY);
            DB.AddInParameter(dbCommand, "GZTD", DbType.Int16, obj.GZTD);
            DB.AddInParameter(dbCommand, "otherRemark", DbType.String, obj.otherRemark);
            DB.AddInParameter(dbCommand, "ZDLX", DbType.Int16, obj.ZDLX);
            DB.AddInParameter(dbCommand, "JSBY", DbType.Int16, obj.JSBY);
            DB.AddInParameter(dbCommand, "CGZF", DbType.Int16, obj.CGZF);
            DB.AddInParameter(dbCommand, "PJJG", DbType.Int16, obj.PJJG);
            DB.AddInParameter(dbCommand, "U4RID", DbType.Int32, obj.U4RID);

            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
    }
}
